<template>
  <el-card class="box-card">
    <el-table :data="records" border style="width: 100%">
      <el-table-column
        prop="index"
        label="序号"
        width="50"
        type="index"
      ></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width=""></el-table-column>
      <el-table-column prop="skuDefaultImg" label="默认图片" width="">
        <template v-slot="{ row }">
          <img
            :src="row.skuDefaultImg"
            :alt="row.skuName"
            style="width:150px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量(KG)"
        width=""
      ></el-table-column>
      <el-table-column prop="price" label="价格(元)" width=""></el-table-column>
      <el-table-column prop="deacides" label="操作" width="">
        <template v-slot="{ row }">
          <el-button
            type="success"
            size="mini"
            v-show="!row.isSale"
            @click="onSaleSku(row)"
          >
            上架
          </el-button>
          <el-button
            type="info"
            size="mini"
            v-show="!!row.isSale"
            style="margin-left:0"
            @click="canceSaleSku(row)"
          >
            下架
          </el-button>
          <el-button type="warning" size="mini">修改</el-button>
          <el-button type="danger" size="mini" @click="delSku(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,sizes,total"
      :total="total"
    ></el-pagination>
  </el-card>
</template>

<script>
import { getSkuList, deleteSku, onSale, canceSale } from '@/api/sku'

export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 5,
      records: [],
      total: 0
    }
  },
  async mounted() {
    this.getSku()
  },
  methods: {
    //请求
    async getSku() {
      let { page, limit } = this
      let res = await getSkuList({ page, limit })
      console.log(res)
      this.total = res.total
      this.records = res.records
    },

    //删除
    async delSku(row) {
      // console.log()
      try {
        await deleteSku(row.id)
        this.getSku()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } catch (error) {
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      }
    },
    handleSizeChange(val) {
      this.limit = val
      this.getSku()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getSku()
    },

    //上架
    async onSaleSku(row) {
      try {
        await onSale(row.id)
        this.getSku()
        this.$message({
          message: '操作成功',
          type: 'success'
        })
      } catch (error) {
        this.$message({
          message: '操作失败',
          type: 'error'
        })
      }
    },

    //下架
    async canceSaleSku(row) {
      try {
        await canceSale(row.id)
        this.getSku()
        this.$message({
          message: '操作成功',
          type: 'success'
        })
      } catch (error) {
        this.$message({
          message: '操作失败',
          type: 'error'
        })
      }
    }
  }
}
</script>

<style scoped>
.box-card {
  margin: 10px;
}
</style>
